/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.source.web.video;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.I18nUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.source.entity.video.EleSourceVideo;
import com.thinkgem.jeesite.modules.source.entity.video.EleSourceVideoGroup;
import com.thinkgem.jeesite.modules.source.service.video.EleSourceVideoGroupService;
import com.thinkgem.jeesite.modules.source.service.video.EleSourceVideoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 视频资源管理Controller
 * @author ZC
 * @version 2017-06-16
 */
@Controller
@RequestMapping(value = "${adminPath}/source/video/eleSourceVideoGroup")
public class EleSourceVideoGroupController extends BaseController {

	@Autowired
	private EleSourceVideoGroupService eleSourceVideoGroupService;
    @Autowired
    private EleSourceVideoService eleSourceVideoService;

	@ModelAttribute
	public EleSourceVideoGroup get(@RequestParam(required=false) String id) {
		EleSourceVideoGroup entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eleSourceVideoGroupService.get(id);
		}
		if (entity == null){
			entity = new EleSourceVideoGroup();
		}
		return entity;
	}
	
	@RequiresPermissions("source:video:eleSourceVideoGroup:view")
	@RequestMapping(value = {"list", ""})
	public String list(EleSourceVideoGroup eleSourceVideoGroup, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EleSourceVideoGroup> page = eleSourceVideoGroupService.findPage(new Page<EleSourceVideoGroup>(request, response), eleSourceVideoGroup); 
		model.addAttribute("page", page);
		return "modules/source/video/eleSourceVideoGroupList";
	}

	@RequiresPermissions("source:video:eleSourceVideoGroup:view")
	@RequestMapping(value = "form")
	public String form(EleSourceVideoGroup eleSourceVideoGroup, Model model) {
		System.out.println(eleSourceVideoGroup.getEleSourceVideoList().size());
		model.addAttribute("eleSourceVideoGroup", eleSourceVideoGroup);
		return "modules/source/video/eleSourceVideoGroupForm";
	}

	@RequiresPermissions("source:video:eleSourceVideoGroup:view")
	@RequestMapping(value = "preview")
	public String preview(String groupId, Model model) {
        List<EleSourceVideo> videoList = eleSourceVideoService.findList(new EleSourceVideo(new EleSourceVideoGroup(groupId)));
	    for(EleSourceVideo video:videoList)
        {
            String path = video.getPath();
            video.setPath(path.replace("|",""));
            video.setName(path.substring(path.indexOf(".") + 1));
        }
        model.addAttribute("videoList", videoList);

        return "modules/source/video/eleSourceVideoPreview";
	}

	@RequiresPermissions("source:video:eleSourceVideoGroup:edit")
	@RequestMapping(value = "save")
	public String save(EleSourceVideoGroup eleSourceVideoGroup, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, eleSourceVideoGroup)){
			return form(eleSourceVideoGroup, model);
		}
		for(EleSourceVideo esv:eleSourceVideoGroup.getEleSourceVideoList()){
			esv.setGroup(eleSourceVideoGroup);
		}
		eleSourceVideoGroupService.save(eleSourceVideoGroup);
		String locale = I18nUtils.getLocaleStr();
		String msg = "保存视频资源成功";
		if(Global.US.equals(locale)){
			msg = "video saved";
		}
		addMessage(redirectAttributes, msg);
		return "redirect:"+Global.getAdminPath()+"/source/video/eleSourceVideoGroup/?repage";
	}
	
	@RequiresPermissions("source:video:eleSourceVideoGroup:edit")
	@RequestMapping(value = "delete")
	public String delete(EleSourceVideoGroup eleSourceVideoGroup, RedirectAttributes redirectAttributes) {
		eleSourceVideoGroupService.delete(eleSourceVideoGroup);
		String locale = I18nUtils.getLocaleStr();
		String msg = "删除视频资源成功";
		if(Global.US.equals(locale)){
			msg = "video deleted";
		}
		addMessage(redirectAttributes, msg);
		return "redirect:"+Global.getAdminPath()+"/source/video/eleSourceVideoGroup/?repage";
	}

}